import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
   //     m1();
       // m2();
       // m3();
        //m4();
        m5();
    }
    private static void m1() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            try {
                connection = DriverManager.getConnection(url, "root", "3390956");
                // if(connection != null){
                System.out.println("连接成功");
                // }
                //3.sql
                String sql = "select * from user";
                //4.预编译sql
                preparedStatement = connection.prepareStatement(sql);
                resultSet = preparedStatement.executeQuery();
                //6.
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    String name = resultSet.getString("name");
                    String pass = resultSet.getString("pass");
                    System.out.println("id" + "----" + name + "----" + pass);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {

            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }
    private static void m2() {
        //键盘录入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查询的用户名：");
        String name = scanner.nextLine();

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {

            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            try {
                connection = DriverManager.getConnection(url, "root", "3390956");
                // if(connection != null){
                System.out.println("连接成功");
                // }
                //3.sql
                Class.forName("com.mysql.cj.jdbc.Driver");
                String sql = "select * from user where name like ? ";
               preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, "%" + name + "%");
                 resultSet = preparedStatement.executeQuery();
                //4.预编译sql

                //6.
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    String username = resultSet.getString("name");
                    String pass = resultSet.getString("pass");
                    System.out.println("ID:" + id + ",Name :+ " + pass);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {

            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }
    private static void m3() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            try {
                connection = DriverManager.getConnection(url, "root", "3390956");
                // if(connection != null){
                //System.out.println("连接成功");
                // }
                //3.sql
                String sql = "insert into user(name,pass)values(?,?)";
                //4.预编译sql
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, "张三");
                preparedStatement.setString(2, "123456");
                int result = preparedStatement.executeUpdate();
                if (result > 0) {
                    System.out.println("添加成功");
                } else {
                    System.out.println("添加失败");
                }
            }

            catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {

            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }
    private static void m4() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            try {
                connection = DriverManager.getConnection(url, "root", "3390956");
                // if(connection != null){
               // System.out.println("连接成功");
                // }
                //3.sql
                String sql = "update user set name=?,pass=?where id=?";
                //4.预编译sql
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, "张三");
                preparedStatement.setString(2, "888");
                preparedStatement.setInt(3, 7);
                int result = preparedStatement.executeUpdate();
                if (result > 0) {
                    System.out.println("修改成功");
                } else {
                    System.out.println("修改失败");
                }


            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {

            try {
                if (preparedStatement != null) {
                   preparedStatement.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }
    private static void m5() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            try {
                connection = DriverManager.getConnection(url, "root", "3390956");
                // if(connection != null){
                // System.out.println("连接成功");
                // }
                //3.sql
                String sql = "delete from user where id=?";
                //4.预编译sql
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setObject(1,7);
                int result = preparedStatement.executeUpdate();
                if (result > 0) {
                    System.out.println("删除成功");
                } else {
                    System.out.println("删除失败");
                }


            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {

            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }
                   }